<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2020/2/10
 * Time: 14:11
 * Note: model.php
 */

namespace app\lib;


use Ext\libMySQL;
use PDO;

class model extends libMySQL
{
    /**
     * model constructor.
     */
    public function __construct()
    {
        $this->setTablePrefix('g_')->setTableName()->bindLibPdo(base::new()->lib_pdo);
    }

    /**
     * 是否存在，与where连用
     *
     * @return bool
     */
    public function exist(): bool
    {
        return !empty($this->select()->limit(1)->getRow());
    }

    /**
     * 求和
     *
     * @return mixed
     */
    public function cnt()
    {
        return current($this->select('COUNT(*) AS C')->limit(1)->getRow(PDO::FETCH_COLUMN));
    }

    /**
     * 读取一行，与where，limit连用
     *
     * @param string $field
     * @param int    $fetch_style
     *
     * @return array
     */
    public function read(string $field = '*', int $fetch_style = PDO::FETCH_ASSOC): array
    {
        return $this->select($field)->getRow($fetch_style);
    }

    /**
     * 读取一个固定字段，与where，limit连用
     *
     * @param string $field
     * @param int    $fetch_style
     *
     * @return mixed
     */
    public function read_val(string $field = '*', int $fetch_style = PDO::FETCH_ASSOC)
    {
        return current($this->select($field)->getRow($fetch_style));
    }

    /**
     * 读取多行，与where，limit连用
     *
     * @param string $field
     * @param int    $fetch_style
     *
     * @return array
     */
    public function read_all(string $field = '*', int $fetch_style = PDO::FETCH_ASSOC): array
    {
        return $this->select($field)->getAll($fetch_style);
    }

    /**
     * 保存，与value连用
     *
     * @param array $data
     *
     * @return bool
     */
    public function save(array $data): bool
    {
        return $this->insert($data)->execute();
    }

    /**
     * 不存在则写入，存在就读取指定主键，只适用于自增主键
     *
     * @param array  $where
     * @param array  $data
     * @param string $id_field
     *
     * @return int
     */
    public function save_nx(array $where, array $data, string $id_field): int
    {
        //获取主键
        $id_res = $this->select($id_field)->where($where)->limit(1)->getRow();

        if (empty($id_res)) {
            //不存在，写入
            return $this->insert($data)->execute() ? $this->getLastInsertId() : 0;
        } else {
            //存在，直接返回id
            return (int)current($id_res);
        }
    }

    /**
     * 替换式写入，存在则更新，不存在则插入
     *
     * @param array $data
     * @param array $field_value
     *
     * @return bool
     */
    public function save_rp(array $data, array $field_value = []): bool
    {
        if (empty($field_value)) {
            //插入
            return $this->insert($data)->execute();
        } else {
            //更新
            return $this->update($data)->where($field_value)->execute();
        }
    }

    /**
     * 只更新，要与where和value连用
     *
     * @param array $data
     *
     * @return bool
     */
    public function save_up(array $data): bool
    {
        return $this->update($data)->execute();
    }

    /**
     * 表别名
     *
     * @param $alias
     *
     * @return $this
     */
    public function alias($alias): self
    {
        return $this->setTableName(get_called_class() . ' AS ' . $alias);
    }

    /**
     * 删除，要与where连用
     *
     * @return bool
     */
    public function del(): bool
    {
        return $this->delete()->execute();
    }

    public function get_page(int $page, int $page_size): array
    {
        $data                       = [
            'curr_page' => $page,
            'cnt_data'  => 0,
            'cnt_page'  => 0,
            'list'      => []
        ];
        $page                       = $page < 1 ? 1 : $page;
        $page_size                  = $page_size < 1 ? 1 : $page_size;
        $this->runtime_data['cols'] = "SQL_CALC_FOUND_ROWS " . $this->runtime_data['cols'];
        $data['list']               = $this->limit(($page - 1) * $page_size, $page_size)->getAll();
        $data['cnt_data']           = model::new()->query("select FOUND_ROWS() as c")->fetch(\PDO::FETCH_COLUMN);
        $data['cnt_page']           = ceil($data['cnt_data'] / $page_size);
        return $data;
    }
}